#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "iNEMO_SDK.h"



INEMO_ERROR INEMO_SendFrame( INEMO_DEVICE_HANDLE hDeviceHandle, char *frame, unsigned int frameSize ) {
    write( hDeviceHandle, frame, frameSize );
    return 0;
}

INEMO_ERROR INEMO_receiveFrame( INEMO_DEVICE_HANDLE hDeviceHandle, char *frame, unsigned int *frameSize ) {
    int readnum = read( hDeviceHandle, frame, *frameSize ); 
    if( readnum != -1 ) {
        *frameSize = readnum; return 0; }
    return -1;
}

INEMO_ERROR INEMO_SendFrameBase( INEMO_DEVICE_HANDLE hDeviceHandle, char *frame, int frameSize,
        char *frameRespond, int *frameRespondSize ) {
    int err;
    if( -1 == (err = write( hDeviceHandle, frame, frameSize )) ) {
        perror("Failed to write Com ");
        return -1; // write error
    }
    
    if( 0 >= (err = read( hDeviceHandle, frameRespond, *frameRespondSize )) )
    {
        if( err == -1 )
        {
            perror( "Failed to read com" );
            return -1;  //read empty;
        }
    }

    *frameRespondSize = err ;

    return 0;
}

INEMO_ERROR INEMO_Start_Acquision( INEMO_DEVICE_HANDLE hDeviceHandle )
{
    int err;
    char frame[] = { 0xaa, 0x20, 0x01, 0x52, 0xaa };
    unsigned char frameRespond[10];

    if ( -1 == (err = write(hDeviceHandle, frame, 5)) )
    {
        perror("Failed to start acquision");
        return -1;
    }

    if( 3 == ( err = read( hDeviceHandle, frameRespond, 3 ) ) )
    {
        if( frameRespond[0] == 0x80 && frameRespond[1] == 0x01 && frameRespond[2] == 0x52 )
            return 0;
    }
    return -1;
        
}
